﻿using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;

using System;
using System.Collections.Generic;
using System.Linq;

namespace NRails
{
  public module Exts
  {
      public With[T](this src : array[T], withMember : T) : array[T]
      {
          def src = if (src != null) src else array[];
          
          def dst = array(src.Length+1);
          Array.Copy(src, dst, src.Length);
          dst[dst.Length - 1] = withMember;
          dst
      }

      public Without[T](this src : array[T], withMember : T) : array[T]
      {
          def src = if (src != null) src else array[];
          def index = Array.IndexOf(src, withMember);
          if (index >= 0)
          {
              def dst = array(src.Length-1);
              Array.Copy(src, dst, index);
              
              when (index < src.Length)
                Array.Copy(src, index + 1, dst, index, dst.Length - index);
                
              dst;
          }
          else
            src;
      }
      
      public Replace[T](this src : array[T], oldItem : T, newItem : T) : void
      {
          def oldIndex = Array.IndexOf(src, oldItem);
          
          src[oldIndex] = newItem;
      }
  }
}
